package io.renren.common.utils;

import io.renren.modules.backstage.dao.RepaymentDao;
import io.renren.modules.backstage.entity.BorrowerEntity;
import io.renren.modules.backstage.entity.RepaymentEntity;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class MyUtils {

    public static List<RepaymentEntity> getOne(BorrowerEntity borrower){
        String str =  borrower.getLoanperiods().substring(0, 1);
        Integer num = Integer.valueOf(str);
        //等额本息
        double invest = borrower.getPrice();     //贷款本金
        double yearRate =borrower.getAnnual()/100;   //年利率
        double monthRate = yearRate / 12;   //月利率

        //             int year = 15;     //还款年数
        int month = num;  //还款月数

        // 每月本金 = 本金×月利率×(1+月利率)^(还款月序号-1)÷((1+月利率)^还款月数-1))
        double monthCapital = 0;
        // 每月利息  = 剩余本金 x 贷款月利率
        double monthInterest = 0;
        double capital = invest;
        double tmpCapital = 0;
        double totalInterest = 0;
        Date time=null;
        ArrayList<RepaymentEntity> list = new ArrayList<>();
        for (int i = 1; i < month + 1; i++) {
            RepaymentEntity repaymentEntity = new RepaymentEntity();
            //添加时间
            if(i==1){
                time=borrower.getStartTime();
                repaymentEntity.setRepayTime(time);
            }
            monthCapital = (invest * monthRate * (Math.pow((1 + monthRate), i - 1))) / (Math.pow(1 + monthRate, month) - 1);
            capital = capital - tmpCapital;
            monthInterest = capital * monthRate;
            tmpCapital = (invest * monthRate * (Math.pow((1 + monthRate), i - 1))) / (Math.pow(1 + monthRate, month) - 1);
            System.out.println("第" + i + "月利息： " + monthInterest);
            totalInterest = totalInterest + monthInterest;
            System.out.println("第" + i + "月本金： " + monthCapital);
            //添加
            Calendar rightNow = Calendar.getInstance();
            rightNow.setTime(time);
            rightNow.add(Calendar.MONTH,1);
            time = rightNow.getTime();
            repaymentEntity.setRepayTime(time);
            //本金
            repaymentEntity.setCapital(monthCapital);
            //利息
            repaymentEntity.setAccrual(monthInterest);
            repaymentEntity.setBorrowId(borrower.getBorrowId());
            repaymentEntity.setPeriods(i);
            list.add(repaymentEntity);
        }
        return  list;
    }

    /**
     * 部分
     * @param borrower
     * @param count
     * @return
     */

    public static List<RepaymentEntity> getThree(BorrowerEntity borrower,Integer count){
        ArrayList<RepaymentEntity> list = new ArrayList<>();
        String str =  borrower.getLoanperiods().substring(0, 1);
        Integer num = Integer.valueOf(str);
        //每月还息一次还本
        //计算每月利息
        Double price= borrower.getPrice()*(borrower.getAnnual()/100/12);
        Date time=null;
        for (int i=1;i<=num-count;i++){
            RepaymentEntity repaymentEntity = new RepaymentEntity();
            if(i==1){
//                        Date NowTime= borrower.getStartTime();
//                        Calendar rightNow = Calendar.getInstance();
//                        rightNow.setTime(NowTime);
//                        rightNow.add(Calendar.MONTH,1);
//                        time = rightNow.getTime();
                time=borrower.getStartTime();
                repaymentEntity.setRepayTime(time);
            }
            if(i<=num-1){
                repaymentEntity.setCapital(0.0);
            }
            if(i==num-count){
                repaymentEntity.setCapital(Double.valueOf(borrower.getPrice()));
            }
            Calendar rightNow = Calendar.getInstance();
            rightNow.setTime(time);
            rightNow.add(Calendar.MONTH,1);
            time = rightNow.getTime();
            repaymentEntity.setRepayTime(time);
            repaymentEntity.setAccrual(price);
            repaymentEntity.setBorrowId(borrower.getBorrowId());
            repaymentEntity.setPeriods(i);
            list.add(repaymentEntity);
        }
        return  list;
    }





}
